﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class FormPlayGame : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString.AllKeys.Contains("gameid"))
                {
                    LoadGame(Convert.ToInt32(Request.QueryString["gameid"]));
                    LoadComments(Convert.ToInt32(Request.QueryString["gameid"]));
                    IncreaseCount(Convert.ToInt32(Request.QueryString["gameid"]));
                }
            }
        }

        private void IncreaseCount(int gameId)
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                dataContext.IncreasePlayedCount(gameId);
            }
        }

        private void LoadGame(int gameId)
        {
            string objectHtml = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"655\" height=\"640\" id=\"{1}\"><param name=\"movie\" value=\"{0}\"><param name=\"quality\" value=\"high\"><param name=\"bgcolor\" value=\"#333399\"><embed src=\"{0}\" quality=\"high\" bgcolor=\"#333399\" width=\"655\" height=\"640\" name=\"{1}\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed></object>";
            string swfPath = "";
            string title = "";
            string imageUrl = "";
            int likeCount = 0;
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                var result = dataContext.GetGameByID(gameId);
                foreach (var item in result)
                {
                    swfPath = item.SwfFile;
                    title = item.Title;
                    likeCount = item.LikeCount;
                    imageUrl = item.ImageUrl;
                }
            }

            lnkLike.Text = string.Format("Like ({0} likes)", likeCount);
            lnkFullScreen.HRef = "FullScreenPlay.aspx?gameid=" + gameId.ToString();
            loadSwf.InnerHtml = string.Format(objectHtml, swfPath, title);
            htitle.InnerText = title;
        }

        private void LoadComments(int gameId)
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                List<Comment> comments = dataContext.Comments.Where(obj => (obj.GameID == gameId)).ToList();
                GridComments.DataSource = comments;
                GridComments.DataBind();
            }
        }

        protected void lnkFavourite_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["fguid"] == null)
            {
                ScriptManager.RegisterStartupScript(pnlUpdateLikes, this.GetType(), "alert", "alert('Please login to add to favourite!')", true);
            }
            else
            {
                string[] splitStr = Request.Cookies["fguid"].Value.Split('/');
                long userId = Convert.ToInt64(splitStr[0]);
                int gameId = Convert.ToInt32(Request.QueryString["gameid"]);
                using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
                {
                    dataContext.AddBookmark(gameId, userId);
                }
            }
        }

        protected void lnkLike_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["fguid"] == null)
            {
                ScriptManager.RegisterStartupScript(pnlUpdateLikes, this.GetType(), "alert", "alert('Please login to like this game!')", true);
            }
            else
            {
                string[] splitStr = Request.Cookies["fguid"].Value.Split('/');
                long userId = Convert.ToInt64(splitStr[0]);
                string username = splitStr[1];
                int gameId = Convert.ToInt32(Request.QueryString["gameid"]);
                using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
                {
                    dataContext.LikeGame(userId, gameId);
                    Common.AddMemberActivity(username, userId, htitle.InnerText, gameId, dataContext.GetGameImage(gameId) , ActivityType.GameLike);
                    lnkLike.Text = string.Format("Like ({0} likes)", dataContext.GetGameLikeCount(gameId));
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["fguid"] == null)
            {
                ScriptManager.RegisterStartupScript(pnlUpdateLikes, this.GetType(), "alert", "alert('Please login to comment this game!')", true);
            }
            else
            {
                string[] splitStr = Request.Cookies["fguid"].Value.Split('/');
                long userId = Convert.ToInt64(splitStr[0]);
                string username = splitStr[1];
                int gameId = Convert.ToInt32(Request.QueryString["gameid"]);
                using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
                {
                    dataContext.AddComment(gameId, txtComment.Text, userId, username);
                    Common.AddMemberActivity(username, userId, htitle.InnerText, gameId, dataContext.GetGameImage(gameId), ActivityType.GameComment);
                }

                LoadComments(gameId);
            }
        }
    }
}